import {Framework} from 'csharp'
import { $promise } from 'puerts';
import {Singleton} from "./Singleton";

export class ResourcesMgr extends Singleton<ResourcesMgr>{

    constructor(){
        super();
    }

    async LoadPrefab(address:string){

        try{
            let task= Framework.ResourceMgr.Instance.LoadPrefab(address);
            let go = await $promise(task);
            return go;
        }catch(ex){

            console.error(`Load prefab :${address} : ${ex}`)

            return null;
        }

    }

    async LoadTextAsset(address:string){

        try{
            let task = Framework.ResourceMgr.LoadTextAsset(address);
            let go = await $promise(task);
            return go;
        }catch(ex){
            console.error(`Load textasset :${address} : ${ex}`)

            return null;
        }
    }


    async LoadTextBytes(address:string){

        try{
            let task = Framework.ResourceMgr.LoadTextBytes(address);
            let bytes = await $promise(task);
            return bytes;
        }catch(ex){
            console.error(`LoadTextBytes :${address} : ${ex}`)
        }
    }

    async LoadSprite(address:string){

        try{
            let task = Framework.ResourceMgr.LoadSprite(address);
            let go = await $promise(task);
            return go;

        }catch(ex){
            console.error(`Load sprite :${address} : ${ex}`)

            return null;
        }
    }

    async RoadFairyGUIPackage(address:string, packageName:string, callback?:Function){

        try{
            let task = Framework.ResourceMgr.LoadFairyGUIPackage(address,packageName);
            await $promise(task);
            
            if(callback) callback();
        }catch(ex){
            console.error(`Load fairyGUI :${address} : ${ex}`)
        }
    }

    public ReleaseAddressGO(go:any){

        Framework.ResourceMgr.ReleaseAddressGO(go);
    }

    public ReleaseFairyGUIPackage(packageName:string){
        Framework.ResourceMgr.ReleaseFGUIPackage(packageName);
    }
}